// 游戏数据层
import { Character, CharacterManager, characterManager } from './character.js';
import { cityManager } from './city.js';

// 导出角色相关类和实例供其他模块使用
export { Character, CharacterManager, characterManager };

// 技能数据
const skillsData = {
    // 大类技能
   耕作: {
        points: 0,
        level: 1,
        talent: 1,
        // 小类技能
        种植: 0,
        畜牧: 0,
        农艺: 0
    },
    采伐: {
        points: 0,
        level: 1,
        talent: 1,
        采集: 0,
        伐木: 0,
        开采: 0,
        勘探: 0
    },
    建设: {
        points: 0,
        level: 1,
        talent: 1,
        建筑: 1,
        修缮: 0
    },
    加工: {
        points: 0,
        level: 1,
        talent: 1,
        冶炼: 0,
        金属锻造: 0,
        纺织: 0,
        食品加工: 0,
        木石加工: 0
    },
    社交: {
        points: 0,
        level: 1,
        talent: 1,
        雄辩: 0,
        交际: 0,
        文书: 0,
        管理: 0
    },
    舟车: {
        points: 0,
        level: 1,
        talent: 1,
        陆上运输: 0,
        水上运输: 0
    },
    畜牧: {
        points: 0,
        level: 1,
        talent: 1,
        饲养: 0,
        繁殖: 0,
        兽医: 0
    }
};

// 游戏状态
const gameState = {
    year: 1,
    season: '春季',
    day: 1,
    // 季节天数
    seasonDays: {
        '春季': 90,
        '夏季': 90,
        '秋季': 90,
        '冬季': 90
    },
    // 当前季节总天数
    currentSeasonTotalDays: 90,
    // 自动保存间隔（天）
    autoSaveInterval: 7
};

// 作物数据
const cropsData = {
    wheat: {
        name: '小麦',
        requiredCapacity: 5,
        minIrrigation: 50,
        maxIrrigation: 200,
        optimalIrrigation: 125,
        minFertility: 30,
        maxFertility: 100,
        optimalFertility: 75,
        minTemperature: 10,
        maxTemperature: 30,
        optimalTemperature: 20,
        growthDays: 90,
        harvestYield: 20
    },
    rice: {
        name: '水稻',
        requiredCapacity: 8,
        minIrrigation: 150,
        maxIrrigation: 300,
        optimalIrrigation: 225,
        minFertility: 40,
        maxFertility: 100,
        optimalFertility: 80,
        minTemperature: 15,
        maxTemperature: 35,
        optimalTemperature: 25,
        growthDays: 120,
        harvestYield: 25
    },
    corn: {
        name: '玉米',
        requiredCapacity: 6,
        minIrrigation: 80,
        maxIrrigation: 220,
        optimalIrrigation: 150,
        minFertility: 35,
        maxFertility: 90,
        optimalFertility: 70,
        minTemperature: 12,
        maxTemperature: 32,
        optimalTemperature: 23,
        growthDays: 100,
        harvestYield: 22
    }
};

// 食材数据
const ingredientsData = {
    // 基础食材（从种植系统获取）
    wheat: {
        name: '小麦',
        type: '谷物',
        level: 1,
        description: '基础谷物，可以制作面包等食物'
    },
    rice: {
        name: '水稻',
        type: '谷物',
        level: 1,
        description: '基础谷物，可以制作米饭等食物'
    },
    corn: {
        name: '玉米',
        type: '谷物',
        level: 1,
        description: '基础谷物，可以煮食或制作其他食物'
    },
    // 加工后的食材
    flour: {
        name: '面粉',
        type: '加工品',
        level: 1,
        description: '由小麦磨制而成',
        craftedFrom: { wheat: 1 }
    },
    cookedRice: {
        name: '米饭',
        type: '加工品',
        level: 1,
        description: '煮熟的米饭',
        craftedFrom: { rice: 1 },
        effects: { stamina: 15, happiness: 5 }
    },
    // 更多食材可以在这里添加
};

// 食谱数据
const recipesData = {
    bread: {
        name: '面包',
        requiredLevel: 1,
        requiredSkill: '食品加工',
        ingredients: { flour: 2 },
        effects: { stamina: 30, happiness: 10, health: 5 },
        description: '简单的面包，提供不错的精力恢复'
    },
    cornSoup: {
        name: '玉米汤',
        requiredLevel: 1,
        requiredSkill: '食品加工',
        ingredients: { corn: 2 },
        effects: { stamina: 25, health: 8, happiness: 3 },
        description: '营养的玉米汤，有助于恢复健康'
    },
    ricePorridge: {
        name: '粥',
        requiredLevel: 1,
        requiredSkill: '食品加工',
        ingredients: { rice: 1 },
        effects: { stamina: 20, health: 3 },
        description: '简单的粥，可以快速恢复一些精力'
    },
    // 更多食谱可以在这里添加
};

// 保存游戏数据
function saveGameData(citiesData = []) {
}


// 重置游戏数据
function resetGameData() {
    console.log('resetGameData');
    // 重置角色数据
    characterManager.characters = [];
    characterManager.activeCharacter = null;
    
    // 重置游戏状态
    gameState.year = 1;
    gameState.season = '春季';
    gameState.day = 1;
    gameState.currentSeasonTotalDays = gameState.seasonDays['春季'];
    
    // 如果城市管理器存在，重置城市
    cityManager.cities = [];
    // 添加一个默认城市
    const defaultCityData = {
        name: '北京',
        climateType: '温带季风',
        currentWeather: '晴',
        currentTemperature: 20,
        buildings: [],
        // 以下用默认的
        availableBuildings: null,
        farm: null,
        land: null
    };
    console.log('call addCity: ', defaultCityData);
    cityManager.addCity({ ...defaultCityData });
    
    // 获取刚创建的城市
    const defaultCity = cityManager.getAllCities()[0];
    // 创建玩家角色并关联城市
    const player = new Character('player1', '玩家', defaultCity);
    characterManager.addCharacter(player);
    // 设置玩家角色为活跃角色
    characterManager.setActiveCharacter('player1');
}

// 导出函数和数据供其他模块使用
export { 
    gameState, 
    cropsData,
    ingredientsData,
    recipesData,
    saveGameData,
    resetGameData
};

// 为了在其他模块中能够访问cityManager（避免循环依赖）
// 在game.js中会将cityManager绑定到window对象